package com.yxkj.railservicer.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yxkj.railservicer.R;
import com.yxkj.railservicer.util.ContentUtil;
import com.yxkj.railservicer.util.JSONUtil;
import com.yxkj.railservicer.util.SPUtil;
import com.yxkj.railservicer.util.ScreenUtils;
import com.yxkj.railservicer.util.SysApplicationUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by sunyu on 2017/3/30.
 */
//tokenUser_url

public class GuideActivity extends AppCompatActivity {

    private OkHttpClient client;
    private JSONUtil jsonUtil;
    private String token;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        SysApplicationUtil.getInstance().addActivity(this);
        ScreenUtils.setTranStatus(this, true, Color.parseColor("#2387f5"));


        client = new OkHttpClient();
        jsonUtil = new JSONUtil();
        token = SPUtil.getString(this, "token");

        Log.e("TAG", "token = " + token);
        if ("".equals(token)) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            judge();
        }
    }

    private void judge() {

        Request request = new Request.Builder()
                .addHeader("token", token)
                .url(ContentUtil.tokenUser_url)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
                        startActivity(intent);
                        finish();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String jsonData = response.body().string();
                Log.e("TAG", "返回数据 = " + jsonData);
                if (jsonUtil.validate(jsonData)) {
                    JSONObject jsonObject = JSON.parseObject(jsonData);
                    if (jsonObject.getBoolean("success")) {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                Intent intent = new Intent(GuideActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        });

                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        });
//                        toast(jsonObject.getString("message"));
                        Log.e("TAG", "message = " + jsonObject.getString("message"));
                    }
                }
            }
        });
    }

    private void toast(final String string) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(GuideActivity.this, string, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SysApplicationUtil.getInstance().deleteActivity(this);
    }
}
